using System;
using System.Runtime.InteropServices;
#if UNITY_IOS
using com.yoozoo.ResourceUpdate;
#endif
using UnityEngine;
using Yoozoo.Gta.Common;
namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx
{
    /// <summary>
    /// 
    /// </summary>
    public class ScreenOrientNativeHelper
    {
#if UNITY_ANDROID
        AndroidJavaObject activityObject;
#endif
        /// <summary>
        /// 玩家设置屏幕方向后，同步给Native层
        /// </summary>
        /// <param name="orientation"></param>
        public  void SaveToLocal(UnityEngine.ScreenOrientation orientation)
        {
           
            //保存到Android
#if UNITY_ANDROID && !UNITY_EDITOR
            if (activityObject == null)
            {
                var activityClass = new AndroidJavaClass("com.yoozoo.codeg.CustomUnityActivity");
                activityObject = activityClass.CallStatic<AndroidJavaObject>("GetCurrentActivity");
            }
            if (activityObject != null)
            {
                var result = activityObject.Call<Boolean>("saveCurrentScreenOrientation", (int)orientation);
                if (result)
                {
                    Debug.Log("保存成功");
                }
                else
                {
                    Debug.Log("保存失败");
                }
            }
            else
            {
                Debug.LogError("ScreenOrientation   activityObject is null");
            }
#elif UNITY_IOS && !UNITY_EDITOR
            if (GameHelper.GetAOTCodeVersion() >= 20241031){
                //保存到IOS
                _saveToLocalIos((int)orientation);
            }
#elif UNITY_STANDALONE_WIN
            Debug.Log("Windows平台不支持保存屏幕方向");
#endif
        }
#if UNITY_IOS && !UNITY_EDITOR
        private void _saveToLocalIos(int orientation)
        {
             ScreenOrientNativeHelper_AOT _screenOrientNativeHelperAOT = new ScreenOrientNativeHelper_AOT();
             _screenOrientNativeHelperAOT.SaveToLocal((UnityEngine.ScreenOrientation)orientation);   
        }
#endif
    }
}